package com.qdlc.p2p.manage.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.SystemConfigService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.BorrowStyle;
import com.qdlc.p2p.common.constant.BorrowTimeType;
import com.qdlc.p2p.common.util.HttpUtil;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.BorrowDao;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.SystemConfig;
import com.qdlc.p2p.dal.model.SystemConfigModel;

/**
 * 系统参数管理
 * 
 * @author xx
 * @version 2.0
 * @since 2014年4月21日
 */
public class SystemConfigAction extends BaseAction<SystemConfigModel> implements ModelDriven<SystemConfigModel> {

	@Resource
	private SystemConfigService systemConfigService;
	
	@Resource
	private BorrowDao borrowDao;

	private Map<String, Object> map = new HashMap<String, Object>();

	/**
	 * 列表页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/system/sconfig/sconfigManager")
	public String sconfigManager() throws Exception {
		return "sconfigManager";
	}

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@Action("/modules/system/sconfig/sconfigList")
	public void sconfigList() throws Exception {
		if ("".equals(paramString("status"))) {
			model.setStatus(99);
		}
		if ("".equals(paramString("type"))) {
			model.setType(99);
		}
		else
			model.setType(Integer.valueOf(paramString("type")));
		PageDataList<SystemConfigModel> pageList = systemConfigService.list(model);
		int totalPage = pageList.getPage().getTotal(); // 总页数
		map.put("total", totalPage);
		map.put("rows", pageList.getList());
		printJson(getStringOfJpaObj(map));
	}

	/**
	 * 新增页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/system/sconfig/sconfigAddPage")
	public String sconfigAddPage() throws Exception {
		saveToken("configAddToken");
		return "sconfigAddPage";
	}
	/**
	 * 校验标识是否已存在
	 * @throws Exception
	 */
	@Action("/modules/system/sconfig/checkNid")
	public void checkNid() throws Exception {
		systemConfigService.checkNid(model.getNid());
		printSuccess();
	}
	/**
	 * 新增
	 * 
	 * @throws Exception
	 */
	@Action("/modules/system/sconfig/sconfigAdd")
	public void sconfigAdd() throws Exception {
		checkToken("configAddToken");
		systemConfigService.checkNid(model.getNid());
		systemConfigService.add(model.prototype());
		printResult(MessageUtil.getMessage("I10001"), true);
	}

	/**
	 * 修改页面
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/modules/system/sconfig/sconfigEditPage")
	public String sconfigEditPage() throws Exception {
		SystemConfig sconfig = systemConfigService.find(model.getId());
		request.setAttribute("sconfig", sconfig);
		return "sconfigEditPage";
	}

	/**
	 * 获取SEO管理
	 *
	 * @return SEO管理页面
	 * @throws Exception 异常
	 */
	@Action(value = "/modules/system/sconfig/sconfigSEO")
	public String SEO() throws Exception {
		return "sconfigSEO";
	}


	/**
	 *
	 * @return SEO添加页面
	 * @throws Exception 异常
	 */
	@Action(value = "/modules/system/sconfig/SEOAddPage")
	public String SEOadd() throws Exception {
		saveToken("configAddToken");
		return "SEOAddPage";
	}

	/**
	 *
	 * @return SEO编辑页面
	 * @throws Exception 异常
	 */
	@Action(value = "/modules/system/sconfig/SEOEditPage")
	public String SEOEdit() throws Exception {
		SystemConfig sconfig = systemConfigService.find(model.getId());
		request.setAttribute("sconfig", sconfig);
		return "SEOEditPage";
	}




	/**
	 * 修改
	 * 
	 * @throws Exception
	 */
	@Action("/modules/system/sconfig/sconfigEdit")
	public void sconfigEdit() throws Exception {
		systemConfigService.update(model.prototype());
		// 修改所有体验标为一次性还款天标，期限由后台参数控制
		if ("con_experience_time_limit".equals(model.getNid())) {
			List<Borrow> experienceBorrwList = borrowDao.findByProperty("category", 2);
			for (Borrow eb : experienceBorrwList) {
				eb.setBorrowTimeType(BorrowTimeType.DAY);
				eb.setTimeLimit(Integer.valueOf(model.getValue()));
				eb.setStyle(BorrowStyle.STYLE_ONETIME_REPAYMENT);
				borrowDao.update(eb);
			}
		}
		SystemConfigHelper.update(model.getNid());
		try {
			this.clean();// 修改就清前台缓存
		} catch (Exception ex) {
			// 更新缓存失败
		}
		printResult(MessageUtil.getMessage("I10002"), true);
	}

	@Action("/modules/system/sconfig/clean")
	public void clean() throws Exception{
		//后台缓存清理
		systemConfigService.clean();
		Map<String, String> params = new HashMap<String, String>();
		params.put("test", "test");//params不能为空，随便设置参数
		//前台路径
		String web_url = SystemConfigHelper.getValue(SystemConfigHelper.Nid.WEB_URL);
		String url = web_url+"/system/clean.html";
		//前台缓存清理
		String result;
        if(web_url.startsWith("https"))
        {
            result = HttpUtil.getHttpsResponse(url);
        } else {
            result = HttpUtil.getHttpResponse(url);
        }
		if(result.contains("ok")){
			printSuccess();
		} 
	}

	@Action("/modules/system/sconfig/cleanLink")
	public void cleanLink() throws Exception {
		// 后台缓存清理
		systemConfigService.cleanLink();
		// 前台路径
		String web_url = SystemConfigHelper.getValue(SystemConfigHelper.Nid.WEB_URL);
		String url = web_url + "/link/clean.html";
		String result;
		if(web_url.startsWith("https"))
		{
			result = HttpUtil.getHttpsResponse(url);
		} else {
			result = HttpUtil.getHttpResponse(url);
		}
		if (result.contains("ok")) {
			printSuccess();
		}
	}
}
